<?php
namespace Api\Middlewares;

use AllowDynamicProperties;
use Api\traits\ApiResponse;
use Closure;
use Illuminate\Support\Facades\Auth;
use Modules\Member\Models\Members;

#[AllowDynamicProperties] class CheckUserLogin
{

    use ApiResponse;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $token = $request->header('token');

        if(empty($token)){
            return $this->fail('token无效',[],999);
        }
        $userInfo = Members::where('token', $token)->first();
        if(empty($userInfo)){
            return $this->fail('token不匹配',[],999);
        }
        $request->userInfo = $userInfo;
        $request->userId = $userInfo->id;
        return $next($request);
    }
}
